<template>
    <div class="home-page">
        <section class="py-5 text-center container">
            <div class="row py-lg-5">
                <div class="col-lg-6 col-md-8 mx-auto">
                    <img src="../assets/callout.svg" alt="callout" class="w-50" />
                    <h2 class="font-weight-light">随心写作，自由表达</h2>
                    <p>
                        <router-link to="/create" class="btn btn-primary my-2">开始写文章</router-link>
                    </p>
                </div>
            </div>
        </section>
        <uploader action="/upload" :beforeUpload="beforeUpload" @file-uploaded="onFileUploaded">
            <template #uploaded="dataProps">
                <img :src="dataProps.uploadedData.data.url" width="500">
            </template>
        </uploader>
        <h4 class="font-weight-bold text-center">发现精彩</h4>
        <column-list :list="list"></column-list>
    </div>
</template>

<script lang="ts">
import { defineComponent, computed, onMounted } from 'vue'
import ColumnList from '../components/ColumnList.vue'
import Uploader from "../components/Uploader.vue";
import createMessage from '@/components/createMessage';
import { GlobalDataProps, ResponseType, ImageProps } from '../store/index'
import { useStore } from 'vuex';
export default defineComponent({
    name: 'Home',
    components: {
        ColumnList,
        Uploader
    },
    setup() {
        const store = useStore()
        const list = computed(() => store.state.columns)
        onMounted(() => {
            store.dispatch('fetchColumns')
        })
        const beforeUpload = (file: File) => {
            const isJPG = file.type === 'image/jpeg'
            if (!isJPG) {
                createMessage('上传图片只能是JPG格式！', 'error')
            }
            return isJPG
        }
        const onFileUploaded = (res: ResponseType<ImageProps>) => {
            createMessage(`上传图片ID ${res.data._id}`, 'success')
        }
        return {
            list,
            beforeUpload,
            onFileUploaded
        }
    }
})
</script>